package mypackage.stream;

import java.io.IOException;
import javax.microedition.io.Datagram;
import javax.microedition.io.DatagramConnection;
import mypackage.EchoScreen;

public class RecvBuffer
{
    private DatagramConnection mConnection;

    private byte[] buffer;
    
    private Datagram mPacket;

    private Thread bufferThread = new Thread()
    {
        public void run()
        {
            try
            {
                mConnection.receive(mPacket);
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            
            byte[] data = mPacket.getData();
            System.arraycopy(data, 0, buffer, 0, data.length);
        };
    };

    public RecvBuffer(DatagramConnection connection, int bufferSize)
    {
        mConnection = connection;
        buffer = new byte[bufferSize];
    }

    public void startBuffer()
    {
        try
        {
            mPacket = mConnection.newDatagram(mConnection.getMaximumLength());
        }
        catch (IOException e1)
        {
            EchoScreen.log("recv init udp exception:" + e1.toString());
            e1.printStackTrace();
        }
        bufferThread.start();
    }
}
